package com.dodock.android.banglapapers.ui;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.dodock.android.banglapapers.MyApplication;
import com.dodock.android.banglapapers.R;
import com.google.ads.AdRequest;
import com.google.ads.AdView;
import com.google.analytics.tracking.android.GoogleAnalytics;
import com.google.analytics.tracking.android.Tracker;

public class SettingsActivity extends Activity implements OnClickListener {

	private final int FIRST_MENU_ID = Menu.FIRST;
	private final int HELP_MENU_ITEM = FIRST_MENU_ID + 1;
	private final int SEND_MENU_ITEM = FIRST_MENU_ID + 2;
	private final int RATE_MENU_ITEM = FIRST_MENU_ID + 3;
	private final int ABOUT_MENU_ITEM = FIRST_MENU_ID + 4;
	
	private Tracker mGaTracker;
	private GoogleAnalytics mGaInstance;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
				
		//Google Analytics
		try{
		    mGaInstance = GoogleAnalytics.getInstance(this);
		    mGaTracker = mGaInstance.getTracker(getResources().getString(R.string.ga_trackingId));
		}
		catch (Exception e) {
		}
		//--Google Analytics
		
		
		setContentView(R.layout.settings);
		
		AdView adView = (AdView) this.findViewById(R.id.adView);
		adView.loadAd(new AdRequest());
		
		Button btn = (Button) findViewById(R.id.about);
		btn.setOnClickListener(this);

		btn = (Button) findViewById(R.id.send);
		btn.setOnClickListener(this);

		btn = (Button) findViewById(R.id.help);
		btn.setOnClickListener(this);

		btn = (Button) findViewById(R.id.rate_application);
		btn.setOnClickListener(this);
		
		Button topNewsButton = (Button) findViewById(R.id.topNews);
		topNewsButton.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(SettingsActivity.this, TopNewsActivity.class);
				startActivity(intent);
			}
		});

		Button favouritButton = (Button) findViewById(R.id.favorite);
		favouritButton.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(SettingsActivity.this, FavouritNewsActivity.class);
				startActivity(intent);
			}
		});

		Button savedNewsButton = (Button) findViewById(R.id.savedNews);
		savedNewsButton.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(SettingsActivity.this, SavedNewsActivity.class);
				startActivity(intent);
			}
		});
	}

	
	@Override
	protected void onResume() {
		super.onResume();
		com.facebook.Settings.publishInstallAsync(this, MyApplication.fb_app_id);
	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		try {
			mGaTracker.sendView("/Settings");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		boolean result = super.onCreateOptionsMenu(menu);
		MenuItem m = menu.add(0, HELP_MENU_ITEM, 0, R.string.help);
		m.setIcon(android.R.drawable.ic_menu_help);
		m = menu.add(0, SEND_MENU_ITEM, 0, R.string.send_to_friend);
		m.setIcon(android.R.drawable.ic_menu_send);
		m = menu.add(0, RATE_MENU_ITEM, 0, R.string.rate);
		m.setIcon(android.R.drawable.btn_star);
		m = menu.add(0, ABOUT_MENU_ITEM, 0, R.string.about);
		m.setIcon(android.R.drawable.ic_menu_info_details);
		return result;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		boolean result = super.onOptionsItemSelected(item);

		switch (item.getItemId()) {

		case ABOUT_MENU_ITEM:
			about();
			break;

		case HELP_MENU_ITEM:
			help();
			break;

		case SEND_MENU_ITEM:
			sendToFriend();
			break;

		case RATE_MENU_ITEM:
			rateApplication();

			break;
		}
		return result;
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.about:
			about();
			break;
		case R.id.help:
			help();
			break;
		case R.id.send:
			sendToFriend();
			break;
		case R.id.rate_application:
			rateApplication();
			break;
		default:
			break;
		}
	}

	private void rateApplication() {
		Intent i = new Intent(Intent.ACTION_VIEW);
		i.setData(Uri.parse("market://details?id=" + getPackageName()));
		startActivity(i);
	}

	private void sendToFriend() {
		Intent intent = new Intent(this, SendToFriendActivity.class);
		startActivity(intent);
	}

	private void help() {
		Intent intent = new Intent(this, AboutHelpActivity.class);
		intent.putExtra("screen", "help");
		startActivity(intent);
	}

	private void about() {
		Intent intent = new Intent(this, AboutHelpActivity.class);
		intent.putExtra("screen", "about");
		startActivity(intent);
	}
}
